'''
Created on 2012-3-8

@author: Administrator
'''
#coding=utf-8

from BeautifulSoup import BeautifulSoup

import urllib2
import encodings
import urllib
import sys
import datetime  
import time  
import os
'''

'''
def printfirsturl(url,restaurantname):
    page = urllib2.urlopen(url)
    soup = BeautifulSoup(page,fromEncoding="utf-8")
    print soup.originalEncoding
    doc = soup.prettify()
    getacategorylinkinfo(doc)
   # logfile = open('c:\\write.txt', 'a')
    #print >> logfile, doc.decode('utf-8').encode('gb2312')
    #logfile.close()
    

        
def getacategorylinkinfo(content):
    s = content
    i = s.find('<h2>')
    while  i>0 :
         j = s[i:].find('</h2>')
      #   logfile = open('c:\\write.txt', 'a')
      #   print  >> logfile,  s[i+4:j+i]
      #   logfile.close()
         endofcategory = s[(j+i):].find('</div>')+j+i
         #print s[j+i:endofcategory]
         processlinks_in_a_category(s[j+i:endofcategory])
         s= s[endofcategory:]
         i = s.find('<h2>')
     
def processlinks_in_a_category(content):
        s = content
        i = s.find('<a href="')
        while i>0:
            j = s[i+9:].find('"')
            startofname = s[i+j+9:].find('">')
            endofname = s[i+j+9:].find('</a>')
            name = s[i+j+9+startofname+2:i+j+9+endofname]
           # logfile = open('c:\\write.txt', 'a')
            #print >> logfile,  "http://en.bookfi.org/"+s[i+9:j+i+9]+" "+name
           # logfile.close()
            print "getinfo frome : http://en.bookfi.org/"+s[i+9:j+i+9]
          
            try:
                printsecondurl("http://en.bookfi.org/"+s[i+9:j+i+9],"first")
            except:
                print "error!!"
                continue
            s = s[j+i+9:]
            i = s.find('<a href="')
            
            
def printsecondurl(url,dirname): 
    page = urllib2.urlopen(url)
    getname_linkinfo_of_book(page.read())
    
    
def getname_linkinfo_of_book(content):  
    s = content
    i = s.find('itemprop="name">')
    logfile = open('c:\\write.txt', 'a')
    while  i>0 :
         j=s[i:].find('</h3>')
         bookname = s[i+16:i+j]
         print >> logfile, 'bookname:'+bookname
         s = s[i+j:];
         authorstart = s.find('">')+2
         authorend = s[authorstart:].find('</a>')+authorstart
         author = s[authorstart:authorend]
         print >> logfile, 'author:'+author
         linkstart = s.find('target="_blank" href="')+22;
         linkend = s[linkstart:].find('" title=')+linkstart
         booklink = s[linkstart:linkend]
         #download(booklink)
         print >> logfile,'booklink:'+booklink
         s = s[linkend:]
         booktypestart = s.find('Download  (')
         booktypeend = s.find(')</a>')
         booktype = s[booktypestart+11:booktypeend]
         print >> logfile,'booktype:'+booktype
         print >> logfile,'   '
         s = s[booktypeend:]
         i = s.find('itemprop="name">')
    logfile.close()     

def download(url):
    starttime =  datetime.datetime.now()
    print 'download start time is %s'% starttime
    #urllib.urlretrieve(url,'ffbook.pdf')  
    #os.system('wget %s -a ffbook.pdf' % url)
    request = urllib2.Request(url)
    request.add_header('User-agent', 'Chrome XXX')
    urlretrieve(urllib2.urlopen(request), "ffbook.pdf")
    endtime =  datetime.datetime.now()
    print 'download end time is %s'% endtime   
    print 'you download the file use time %s s' % (endtime - starttime).seconds
'''
    browser = mechanize.Browser()
    print url
    browser.retrieve(url,'ffbook.pdf')
'''
     

      
#printsecondurl('http://en.bookfi.org/Arts-%26-Photography-Architecture-cat1 ','Architecture')        
#processlinksinacategory(content)
printfirsturl("http://en.bookfi.org/categories","first")    

